#pragma once
#include "VariableSymbol.hpp"
#include "Syntax/Object/Object.hpp"

#include <memory>
#include <unordered_map>

class VariableMap
{
    typedef std::shared_ptr<std::unordered_map<SharedVariableSymbol, SharedObject>> SharedVariable;

public:
    VariableMap()
    {
        Variables = std::make_shared<std::unordered_map<SharedVariableSymbol, SharedObject>>();
    }

    //获取指定变量的符号类;
    SharedVariableSymbol GetVarialeSymbol(std::string variableName)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variableName)
                return it->first;
        }
        return nullptr;
    }
    //获取指定变量的值类;
    SharedObject GetVarialeValue(std::string variableName)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variableName)
                return it->second;
        }
        return nullptr;
    }
    // 判断变量是否存在.;
    bool VariableExist(std::string variableName)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variableName)
                return true;
        }
        return false;
    }

    //移除指定变量;
    void RemoveVariable(std::string variableName)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variableName)
            {
                this->Variables->erase(it);
                break;
            }
        }
    }

    //添加变量;
    void Insert(SharedVariableSymbol variable, SharedObject object)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variable->GetName())
                this->Variables->erase(it);
        }

        this->Variables->emplace(variable, object);
    }
    // 给变量赋值;
    void assignValue(SharedVariableSymbol variable, SharedObject object)
    {
        for (auto it = this->Variables->begin(); it != this->Variables->end(); it++)
        {
            if (it->first->GetName() == variable->GetName())
            {

                auto objectbak = it->second;
                it->second = object;

                // 删除以前的;
                objectbak.reset();
                break;
            }
        }

        this->Variables->emplace(variable, object);
    }
    //清空;
    void Clear()
    {
        this->Variables->clear();
    }

private:
    SharedVariable Variables;
};

typedef std::shared_ptr<VariableMap> SharedVariableMap;